package com.example.testapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.Recycler1Activity;

import java.util.ArrayList;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHodler> {
    private final Context context;
    private  ArrayList<String> datas;

    // 第三步 自动生成构造器  之前页点击生成
    public MyRecyclerViewAdapter(Context context, ArrayList<String> datas) {
        this.context = context;
        this.datas = datas;
        
    }
    // 第二步 自动生成的方法

    // 相当于getView方法中创建View和ViewHoler
    /***
     *   相当于ListView 适配中getView的创建holder布局
     * */
    @NonNull
    @Override
    public MyViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.item_recyclerview,null);
        return new MyViewHodler(itemView);
    }
    // 数据和view绑定
    @Override
    public void onBindViewHolder(@NonNull MyViewHodler holder, int position) {
        // 根据位置得到对应数据
        String data = datas.get(position);
        holder.tv_title.setText(data);

    }
    // 得到总条数
    @Override
    public int getItemCount() {
        return datas.size();
    }
    // 添加数据
    public void addData(int i, String data) {
        datas.add(i,data);
        // 刷新设配器
        notifyItemInserted(i);
    }
     // 删除数据
    public void removeData(int i) {
        datas.remove(i);
        // 刷新数据
        notifyItemRemoved(i);

    }

    // 第一步
    class MyViewHodler extends RecyclerView.ViewHolder{
        private ImageView iv_icon;
        private TextView tv_title;
        public MyViewHodler(View itemView){
            super(itemView);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            // 点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Toast.makeText(context, "data==" + datas.get(getLayoutPosition()), Toast.LENGTH_SHORT).show();
                    if(onItemClickListener != null){
                        onItemClickListener.onItemClick(view,datas.get(getLayoutPosition()));
                    }
                }
            });

            // 设置点击图片时触发方法
            iv_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "我是图片==" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    // 点击RecyclerView某条的监听
    public interface OnItemClickListener{
        // 当RecyclerView某个被点击的时候回调
        public void onItemClick(View view,String data);
    }
    private OnItemClickListener onItemClickListener;

    /**
     *  设置某个的监听
     * */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
